package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingPlanDto;
import com.zzyl.dto.NursingProjectPlanDto;
import com.zzyl.entity.NursingPlan;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingPlanService;
import com.zzyl.vo.NursingPlanVo;
import com.zzyl.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class NursingPlanServiceImpl implements NursingPlanService {

    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private NursingProjectPlanMapper projectPlanMapper;
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    /**
     * 护理计划分页查询
     *
     * @return
     */
    @Override
    public PageResponse<NursingPlanVo> getList(String planName, Integer status, Integer pageNum, Integer pageSize) {
        //校验参数
        if (pageNum == null || pageSize == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //设置起始分页
        PageHelper.startPage(pageNum, pageSize);
        Page<NursingPlanVo> page = nursingPlanMapper.selectList(planName, status);
        PageResponse<NursingPlanVo> nursingPlanVos = PageResponse.of(page, NursingPlanVo.class);
        return nursingPlanVos;
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addNursingPlan(NursingPlanDto nursingPlanDto) {
        //参数校验
        if (nursingPlanDto == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        nursingPlan.setStatus(0);//默认停用
        nursingPlanMapper.insertNursingPlan(nursingPlan);
        //新增护理计划关联的护理项目
        List<NursingProjectPlanDto> projectPlans = nursingPlanDto.getProjectPlans();
        //将新增的护理计划返回值ID存入护理计划关联项目中
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        //将List<NursingProjectPlanDto>对象转为List<NursingProjectPlan>对象
        List<NursingProjectPlan> plans = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
        projectPlanMapper.insertprojectPlan(plans);
    }

    /**
     * 查询护理计划信息
     *
     * @param id
     * @return
     */
    @Override
    public NursingPlanVo getById(Long id) {
        if (id == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //先查询护理计划表信息
        NursingPlanVo nursingPlanVo = nursingPlanMapper.getById(id);
        //再查询计划里面关联的项目
        List<NursingProjectPlanVo> projectPlans = projectPlanMapper.selectByPlanId(id);
        nursingPlanVo.setProjectPlans(projectPlans);
        return nursingPlanVo;
    }

    /**
     * 更新护理计划信息
     *
     * @param planDto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateNursingPlan(NursingPlanDto planDto) {
        //参数校验
        if (planDto == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //更新护理计划信息
        NursingPlan nursingPlan = BeanUtil.toBean(planDto, NursingPlan.class);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
        //先删除原有的关联护理项目
        projectPlanMapper.deleteByPlanId(nursingPlan.getId());
        //再重新添加关联护理项目到关联表中
        List<NursingProjectPlanDto> projectPlans = planDto.getProjectPlans();
        projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        //将List<NursingProjectPlanDto>对象转为List<NursingProjectPlan>对象
        List<NursingProjectPlan> plans = BeanUtil.copyToList(projectPlans, NursingProjectPlan.class);
        projectPlanMapper.insertprojectPlan(plans);
    }

    /**
     * 删除护理计划
     *
     * @param id
     */
    @Override
    public void deleteNursingPlan(Long id) {
        if (id == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        nursingPlanMapper.deleteNursingPlan(id);
    }

    //更新启用禁用
    @Override
    public void updateStatus(Long id, Integer status) {
        if (id == null || status == null) {
            throw new BaseException(BasicEnum.PARAM_ERROR);
        }
        //todo 如果护理计划内关联的护理项目是停用状态，则护理计划不能启用
        if (status == 1) { //表示要更改为启用
            //查询到关联护理计划列表
            List<NursingProjectPlanVo> projectPlanVos = projectPlanMapper.selectByPlanId(id);
            List<Long> projectIds = projectPlanVos.stream().map(NursingProjectPlanVo::getProjectId).collect(Collectors.toList());
            //查询护理项目列表
            List<NursingProject> projects = nursingProjectMapper.selectByIds(projectIds);
            if (projects != null && projects.size() != 0) {
                projects.forEach(project -> {
                    //判断是否存在未启用项目
                    if (project.getStatus() == 0) {
                        throw new BaseException(BasicEnum.NURSING_PLAN_FAILED);
                    }
                });
            }
        }
        NursingPlan nursingPlan = new NursingPlan();
        nursingPlan.setStatus(status);
        nursingPlan.setId(id);
        nursingPlanMapper.updateNursingPlan(nursingPlan);
    }

    /**
     * 查询所有护理计划
     * @return
     */
    @Override
    public List<NursingPlanVo> getAllPlan() {
        List<NursingPlanVo> planVos = nursingPlanMapper.selectAllPlan();
        return planVos;
    }
}
